<?php

namespace App\Console\Commands;

use App\Library\Logic\ShopOrderExtLogic;
use App\Models\ShopOrderReturnModel;
use Illuminate\Console\Command;

class SyncOrderReturnNumberToExtCommend extends Command
{
    /**
     * 同步退货序列号到订单扩展表
     *
     * @var string
     */
    protected $signature = 'command:syncOrderReturnNumberToExt';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    public function handle()
    {
        $ext = ShopOrderExtLogic::query()
            ->leftJoin('shop_order_return', 'shop_order_return.order_id', 'shop_order_ext.order_id')
            ->where('status_after_sale', '!=', 0)
            ->whereNull('order_return_number')
            ->get(['shop_order_ext.id', 'shop_order_return.serial_number']);

        if (!is_null($ext)) {
            foreach ($ext as $value) {
                ShopOrderExtLogic::query()->where('id', $value['id'])->update(['order_return_number' => $value->serial_number]);
            }
        }
    }
}
